'use strict';
app.controller('labelItemsCtrl', ['$scope', '$modal','$stateParams', '$log','utilService','labelItemsService','toaster', function($scope, $modal,$stateParams, $log,utilService,labelItemsService,toaster) {
    $scope.id=$stateParams.id;

    //查询该店下的sku分类
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'label/items',
            params:{labelId:$scope.id}
        }).then(function(result){
        });
    }

    $scope.query();


    //打开添加弹窗
    $scope.openEditLabelItems = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/label/label_items_edit_popup.html',
            controller: 'labelItemsEditCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return {id:id};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.query();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

    //打开添加弹窗
    $scope.openAddLabelItems = function () {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/label/label_items_add_popup.html',
            controller: 'labelItemsAddCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return {labelId:$scope.id};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.query();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

    //
    $scope.deleteLabelItems=function(id){
        var param={id:id};
        openModal("请问您确定要删除该属性参数值吗?");
        //打开删除框
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除属性参数值",
                controller:"deleteLabelItemsCtrl",
                message:msg,
                data:param
            });
        }
    }




}]);


app.controller('labelItemsAddCtrl', ['$scope', '$modalInstance','labelItemsService','toaster', 'data',function($scope, $modalInstance,labelItemsService,toaster,data) {
    $scope.labelItemsService=labelItemsService;

    $scope.allowToAddLabel=true;

    $scope.init=function(){
        labelItemsService.labelItems={labelId:data.labelId};
        labelItemsService.systemTypesArr=[];
    }

    $scope.init();

    $scope.addLabelItems=function(){

        if(!$scope.allowToAddLabel){
            return;
        }

        $scope.allowToAddLabel=false;

        labelItemsService.add($scope).then(function(result){
            switch (result.status){
                case 200:
                    $modalInstance.close("success");
                    break;
                case 624:
                case 715:
                    $scope.allowToAddLabel=true;
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });

    }

    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };
}]);


app.controller('labelItemsEditCtrl', ['$scope', '$modalInstance','labelItemsService','toaster','data', function($scope, $modalInstance,labelItemsService,toaster,data) {
    $scope.data=data;

    $scope.labelItemsService=labelItemsService;

    $scope.allowToEditLabel=true;

    $scope.init=function(){
        labelItemsService.label={};
        labelItemsService.systemTypesArr=[];
    }

    $scope.init();

    $scope.editLabelItems=function(){

        if(!$scope.allowToEditLabel){
            return;
        }

        $scope.allowToEditLabel=false;

        labelItemsService.edit($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.allowToEditLabel=true;
                    toaster.pop('success', "提示", "操作成功");
                    $modalInstance.close("success");
                    break;
                case 624:
                case 715:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });

    }



    $scope.findById=function(id){
        labelItemsService.findById($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    labelItemsService.labelItems=result.data;
                    break;
                case 722:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }


    $scope.findById(data.id);


    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };
}]);


app.controller('deleteLabelItemsCtrl', ['$scope', '$modalInstance','labelItemsService','toaster','modalData',function($scope, $modalInstance,labelItemsService,toaster,modalData) {
    $scope.modal = modalData;
    $scope.data=$scope.modal.data;
    $scope.ok=function(){
        debugger;
        labelItemsService.deleteById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $modalInstance.close("关闭");
                    $scope.query();
                    break;
                case 722:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }


    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };
}]);